<template>
  <div class="student-info-page">
    <div class="tims-student-info tims-student-info-page">
      <div class="tims-student-info-body">
        <el-form :model="studentDetailForm" label-position="top" status-icon label-width="100px" class="demo-ruleForm">
          <div class="tims-student-info-subtitle-box">
            <div class="tims-student-info-subtitle">
              <span class="tims-student-info-dot">
                基本信息
              </span>
              <div class="opt-box">
                <el-button class="student-info-edit" v-if="isDisabled()" type="text" @click="changeState('updating')">
                  编辑
                </el-button>
                <el-button class="student-info-edit" v-if="!isDisabled()" type="text" @click="changeState('normal')">
                  取消
                </el-button>
                <el-button class="student-info-back" type="text" @click="goBack()">
                  返回
                </el-button>
              </div>
            </div>

          </div>
          <div class="ku-student-section">
            <el-form-item prop="nickName">
              <div class="photo-image-box">
                <div class="photo-image">
                  <div class="left">
                    <img width="80px" v-if="!studentDetailForm.headImage" height="80px" src="@/assets/head_image.png">
                    <img width="80px" v-else height="80px" :src="studentDetailForm.headImage">
                  </div>
                  <div class="right" v-if="!studentDetailForm.headImage">
                    <div class="bold">上传你的个人头像</div>
                    <div class="normal">上传你的个性头像展示你的个人风格</div>
                  </div>
                </div>
                <div v-if="!isDisabled()">
                  <el-upload :action="uploadUrl" :headers="headers"
                    :on-error="handleUploadError" :on-success="handleUploadSuccess" :show-file-list="false">
                    <el-button type="info">{{ studentDetailForm.headImage ? '更换头像' : '上传头像' }}</el-button>
                    <template #tip> </template>
                  </el-upload>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="昵称" prop="nickName">
              <el-input v-if="!isDisabled()" type="text" v-model="studentDetailForm.nickName" autocomplete="off"
                placeholder="请填写您的昵称" maxlength="8"></el-input>
              <span :class="studentDetailForm.nickName ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.nickName || "请填写您的昵称" }}
              </span>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="studentDetailForm.sex" v-if="!isDisabled()">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="0">女</el-radio>
              </el-radio-group>
              <span :class="studentDetailForm.sex !== null && studentDetailForm.sex !== undefined ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.sex == null ? "请选择您的性别" : +studentDetailForm.sex == 1 ? "男" : "女" }}
              </span>
            </el-form-item>
            <el-form-item label="学校" prop="schoolName">
              <el-input v-if="!isDisabled()" type="text" v-model="studentDetailForm.schoolName" autocomplete="off"
                placeholder="请填写您的学校名称"></el-input>
              <span :class="studentDetailForm.schoolName ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.schoolName || "请填写您的学校名称" }}
              </span>
            </el-form-item>
            <el-form-item label="专业" prop="majorName">
              <el-input v-if="!isDisabled()" type="text" v-model="studentDetailForm.majorName" autocomplete="off"
                placeholder="请填写您的专业名称"></el-input>
              <span :class="studentDetailForm.majorName ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.majorName || "请填写您的专业名称" }}
              </span>
            </el-form-item>
            <el-form-item label="手机" prop="phone">
              <el-input v-if="!isDisabled()" type="text" v-model="studentDetailForm.phone" autocomplete="off"
                placeholder="请填写常用手机号"></el-input>
              <span :class="studentDetailForm.phone ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.phone || "请填写常用手机号" }}
              </span>
            </el-form-item>
            <el-form-item label="常用邮箱" prop="email">
              <el-input v-if="!isDisabled()" type="text" v-model="studentDetailForm.email" autocomplete="off"
                placeholder="请填写常用邮箱"></el-input>
              <span :class="studentDetailForm.email ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.email || "请填写常用邮箱" }}
              </span>
            </el-form-item>
            <el-form-item label="微信号码" prop="wechat">
              <el-input v-if="!isDisabled()" type="text" v-model="studentDetailForm.wechat" autocomplete="off"
                placeholder="请填写微信号码"></el-input>
              <span :class="studentDetailForm.wechat ? 'right-info' : 'right-info gray'" v-else>
                {{ studentDetailForm.wechat || "请填写微信号" }}
              </span>
            </el-form-item>

            <el-form-item label="个人介绍" prop="introduce">
              <el-input v-if="!isDisabled()" :rows="5" type="textarea" v-model="studentDetailForm.introduce"
                autocomplete="off" placeholder="请填写个人介绍" maxlength="80" show-word-limit></el-input>
              <span :class="studentDetailForm.introduce ? 'right-info intro' : 'right-info intro gray'" v-else>
                {{ studentDetailForm.introduce || "请用一段话介绍自己,该简介将会在您的个人主页展示" }}
              </span>
            </el-form-item>
          </div>
          <div class="ku-login-footer" v-if="!isDisabled()">
            <el-button class="ku-login-footer-btn" type="info" plain @click="changeState('normal')">取消</el-button>
            <el-button class="ku-login-footer-btn blue" type="primary" plain @click="editStudent()">保存</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getStudentDetailService,editStudentService, updateHeadImageService } from '@/apis/student'
import { getToken } from '@/utils/cookie'

import router from '@/router'
import { ElMessage } from 'element-plus';
import { reactive, ref } from "vue"

const studentDetailForm = reactive({})
const currentState = ref('normal')

async function getStudentDetail() {
  const studentRef = await getStudentDetailService()
  currentState.value = "normal"
  Object.assign(studentDetailForm, studentRef.data)
}
getStudentDetail()

function goBack() {
  router.go(-1)
}

function changeState(state) {
  currentState.value = state
}

function isDisabled() {
  return currentState.value === "normal"
}

async function editStudent() {
  await editStudentService(studentDetailForm)
  getStudentDetail()
}
//http://127.0.0.1:19090/friend/file/upload
const uploadUrl = ref("/dev-api/file/upload")

const headers = ref({
  Authorization: "Bearer " + getToken(),
})

async function handleUploadSuccess(res) {
  if (res.code !== 1000) {
    ElMessage.error(res.msg)
  } else {
    const studentUpdateDTO = reactive({
      headImage : res.data.name
    })
    await updateHeadImageService(studentUpdateDTO)
    getStudentDetail()
    ElMessage.success("头像上传成功")
  }
}

</script>

<style lang="scss" scoped>
.back-btn-box {
  background: #fff;
  width: 100%;
  height: 48px;
  margin-bottom: 16px;

  .back-btn {
    height: 100%;
    margin: 0 auto;
    display: flex;
    align-items: center;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    cursor: pointer;
    line-height: unset;

    i {
      margin-right: 6px;
      font-size: 16px;
      margin-top: 2.5px;
    }
  }
}

.student-info-page {
  background-color: rgba(247, 247, 247, 1);
  position: relative;
  overflow: hidden;

  :deep(.el-form) {
    .el-button--info {
      background: #f2f3f5;
      border: 1px solid #f2f3f5;
      color: #222;

      &:hover {
        background: #32C5FF;
        border: 1px solid #32C5FF;
        color: #fff;
      }
    }

    .el-form-item {
      max-width: 1120px;
      width: 100%;

      .el-form-item__label {
        font-family: PingFangSC, PingFang SC;
        font-weight: 600;
        font-size: 18px;
        color: #222222;
        margin-bottom: 16px;
      }

      .photo-image-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        padding-bottom: 20px;
        border-bottom: 1px solid #ebebeb;

        .photo-image {
          display: flex;
          flex: 1;
          margin-right: 20px;

          img {
            border-radius: 50%;
          }

          .right {
            flex: 1;
            margin-left: 20px;
            display: flex;
            flex-direction: column;
            justify-content: center;

            .bold {
              height: 25px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 600;
              font-size: 18px;
              color: #222222;
              line-height: 25px;
              margin-bottom: 10px;
            }

            .normal {
              height: 22px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 16px;
              color: #999999;
              line-height: 22px;
            }
          }
        }
      }

      .el-input,
      .el-select,
      .el-radio-group {
        width: 100%;
        height: 44px;
        border-radius: 4px;
        outline-style: none;
        border: none;
        font-size: 16px;

        input {
          color: #222;
        }
      }

      .el-radio-group {

        border-bottom: 1px solid #ebebeb;
      }

      .el-input__wrapper {
        border: none;
        box-shadow: none;
        border-radius: 0;
        padding-left: 0;
        color: #222;
        border-bottom: 1px solid #ebebeb;

        &:hover {
          border-bottom: 1px solid #32c5ff;
        }
      }
    }
  }

  .tims-student-info {
    margin: 0 auto;
    max-width: 1520px;
    width: 100%;

    &.tims-student-info-page {
      background: #f7f7f7;
      margin-bottom: 30px;

      .student-info-back {
        cursor: pointer;
        color: #999999;
        font-size: 15px;
      }

      .student-info-edit {
        cursor: pointer;
        font-size: 15px;
        margin-left: auto;
      }

      .ku-student-section {
        background: #fff;
      }

      .tims-student-info-body {
        padding: 0;

        .tims-student-info-subtitle-box {
          padding: 30px 20px;
          padding-bottom: 0;

          .tims-student-info-subtitle {
            display: flex;
            align-items: center;
            height: 33px;
            font-family: PingFangSC, PingFang SC;
            font-weight: 600;
            font-size: 24px;
            color: #222222;
            line-height: 33px;
          }
        }
      }

      .ku-student-section {
        padding: 30px 24px;
        padding-bottom: 0;
      }

      .ku-student-real-info {
        margin-top: 0;
        margin-bottom: 0;
        padding-left: 24px;
        box-sizing: border-box;
        height: 60px;
        line-height: 60px;
        border-bottom: 1px solid #f2f2f2;
      }

      .el-form-item__content {
        margin-left: 110px;
      }

      .ku-login-footer {
        text-align: left;
        padding-left: 24px;
        padding-bottom: 24px;
      }
    }

    .tims-student-info-title {
      background-color: rgba(255, 255, 255, 1);
      border-radius: 10px;
      width: 100%;
      /* 设置宽度为100%以确保水平居中 */
      height: 60px;
      font-size: 25px;
      text-indent: 20px;
      display: flex;
      align-items: center;
      margin: 20px 0 20px 0;
    }

    .tims-student-info-body {
      background-color: #fff;
      padding: 20px 20px 40px;
      border-radius: 8px;
    }

    .tims-student-info-subtitle {
      font-size: 16px;
      font-weight: 500;
      color: #222222;
      line-height: 25px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .ku-login-footer {
      text-align: center;
      margin-top: 30px;
    }

    .ku-login-footer-btn {
      width: 84px;
      height: 44px;
      border-radius: 3px;
      font-size: 15px;
      font-weight: 400;

      &.blue {
        background: #32C5FF !important;
        color: #fff;
      }
    }

    .ku-student-section {
      background-color: #f7f7f7;
      padding: 30px 38px;
      border-radius: 10px;
    }

    .tims-student-info-action {
      position: relative;
      height: 0;
    }

    .ku-student-validate-phone {
      display: inline-block;
      height: 40px;
      margin-left: 8px;
      color: #24c68b;
      font-weight: 700;
      cursor: pointer;
      student-select: none;
    }

    .ku-student-real-info {
      margin-top: 47px;
    }
  }
}
</style>

<style lang="scss">
.tims-student-info-page {
  .el-form-item__label {
    padding-right: 30px !important;
    color: #666;
  }

  .el-form {
    padding-bottom: 24px;
  }

  .right-info {
    display: inline-block;
    min-width: 90px;
    color: #222;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 16px;
    width: 100%;
    border-bottom: 1px solid #f2f2f2;
    height: 44px;
    line-height: 44px;

    &.gray {
      color: #999;
    }
  }
}
</style>